package com.kwedding.service.web;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kwedding.bean.TB_Images;
import com.kwedding.bean.TB_Style;
import com.kwedding.dao.TB_ImagesMapper;
import com.kwedding.dao.TB_StyleMapper;
import com.kwedding.dto.web.RequestWebAddImages;
import com.kwedding.dto.web.RequestWebBrandImageByType;
import com.kwedding.dto.web.RequestWebGetBrandImages;
import com.kwedding.dto.web.RequestWebUpDownImage;
import com.kwedding.dto.web.RequestWebUpdateBrandImages;
import com.kwedding.dto.web.ResponseWebBrandImages;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.GsonUtil;
import com.kwedding.util.IDGenerate;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.RequestErrorBackException;

import springfox.documentation.spring.web.json.Json;

@Service
public class WebImagesService extends LoggerParent {

	@Autowired
	private TB_ImagesMapper tb_ImagesMapper;
	@Autowired
	private TB_StyleMapper tb_StyleMapper;

	/**
	 * 添加图片
	 * 
	 * @param addImages
	 */
	@Transactional
	public void insertImages(RequestWebAddImages addImages) {
		CheckedUtil.isNull(addImages.getBrandId(), Constants.REQUEST_ERROR_1, "品牌不能为空");
		CheckedUtil.isNull(addImages.getType(), Constants.REQUEST_ERROR_2, "要添加的类型不能为空");
		// tb_Images =
		// tb_ImagesMapper.selectImagesByBrandIdAndType(addImages.getBrandId(),
		// addImages.getType());
		/*
		 * if (tb_Images != null) { throw new
		 * RequestErrorBackException(Constants.REQUEST_ERROR_3,
		 * "同类型图片已存在并且正在使用，请勿重复添加"); }
		 */
		TB_Images tb_Images = CopyerObject.copyerObject(addImages, TB_Images.class);
		tb_Images.setImageAbout(new Json(GsonUtil.listToJsonStr(addImages.getImageAbout())));
		tb_Images.setImageId(IDGenerate.generate());
		tb_Images.setStatus(false);
		tb_Images.setCreateTime(new Date());
		tb_Images.setUpdateTime(new Date());
		if (tb_ImagesMapper.insert(tb_Images) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "添加图片失败");
		}
	}

	/**
	 * 获取品牌图片
	 * 
	 * @param getBrandImages
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<ResponseWebBrandImages> getImageByBrandId(RequestWebGetBrandImages getBrandImages) {
		CheckedUtil.isNull(getBrandImages.getBrandId(), Constants.REQUEST_ERROR_1, "请选择要查看的品牌");
		List<TB_Images> tb_Images = tb_ImagesMapper.selectBrandImages(getBrandImages.getBrandId());
		List<ResponseWebBrandImages> brandImages = new ArrayList<>();
		if (tb_Images != null && tb_Images.size() > 0) {
			tb_Images.stream().forEach(r -> {
				ResponseWebBrandImages images = CopyerObject.copyerObject(r, ResponseWebBrandImages.class);
				images.setIdStr(r.getImageId().toString());
				TB_Style tb_Style = tb_StyleMapper.selectByPrimaryKey(r.getRefId());
				if (tb_Style != null) {
					images.setStyleName(tb_Style.getChineseName());
				}
				brandImages.add(images);
			});
		}
		return brandImages;
	}

	/**
	 * 修改品牌图片
	 * 
	 * @param updateBrandImages
	 */
	@Transactional
	public void updateBrandImages(RequestWebUpdateBrandImages updateBrandImages) {
		CheckedUtil.isNull(updateBrandImages.getImageId(), Constants.REQUEST_ERROR_1, "请先选择要修改的图片");
		TB_Images tb_Images = tb_ImagesMapper.selectByPrimaryKey(updateBrandImages.getImageId());
		if (tb_Images == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_2, "修改的内容不存在");
		}
		tb_Images = CopyerObject.copyerObject(updateBrandImages, TB_Images.class);
		if (updateBrandImages.getImageAbout() != null) {
			tb_Images.setImageAbout(new Json(GsonUtil.listToJsonStr(updateBrandImages.getImageAbout())));
		}
		tb_Images.setUpdateTime(new Date());
		if (tb_ImagesMapper.updateBySelective(tb_Images) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "修改的图片内容失败");
		}
	}

	/**
	 * 
	 * @param brandImages
	 * @return
	 */
	public List<ResponseWebBrandImages> getBrandImageByType(RequestWebBrandImageByType brandImages) {
		CheckedUtil.isNull(brandImages.getBrandId(), Constants.REQUEST_ERROR_1, "请选择要查看的品牌");
		CheckedUtil.isNull(brandImages.getBrandId(), Constants.REQUEST_ERROR_2, "请选择要查看的类型");
		List<TB_Images> tb_Images = tb_ImagesMapper.selectImagesByBrandIdAndType(brandImages.getBrandId(),
				brandImages.getType());
		List<ResponseWebBrandImages> responseWebBrandImages = new ArrayList<>();
		if (tb_Images != null && tb_Images.size() > 0) {
			tb_Images.stream().forEach(r -> {
				ResponseWebBrandImages responseWebBrandImage = CopyerObject.copyerObject(r,
						ResponseWebBrandImages.class);
				responseWebBrandImage.setId(r.getImageId().toString());
				responseWebBrandImages.add(responseWebBrandImage);
			});
		} else {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "该类型暂无图片,请先添加");
		}
		return responseWebBrandImages;
	}

	/**
	 * 上下架图片
	 * 
	 * @param upDownImage
	 */
	public void upDownImage(RequestWebUpDownImage upDownImage) {
		CheckedUtil.isNull(upDownImage.getImageId(), Constants.REQUEST_ERROR_1, "请选择修改的图片");
		TB_Images tb_Images = tb_ImagesMapper.selectByPrimaryKey(upDownImage.getImageId());
		if (tb_Images == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_2, "要修改的内容不存在或已删除");
		}
		tb_Images.setStatus(!tb_Images.getStatus());
		tb_Images.setUpdateTime(new Date());
		if (tb_ImagesMapper.updateBySelective(tb_Images) < 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "上下架图片失败");
		}
	}
}
